import { App, Checkbox, Col, Descriptions, Form, Input, InputNumber, Modal, Radio, Row, Select, Tooltip } from 'antd';
import { useState, useEffect } from 'react'
import FetchHelper from '../../../../../../utils/helper/fetch-helper';
import MapPoint from '../../../../../framework/MapPoint';
import { getFields } from '../fields';
import XytjApi from '../../../../../../utils/apis/tj/XytjApi';
import XztjApi from '../../../../../../utils/apis/tj/XztjApi';

const A = props => {
    const { value, onChange, tooltip } = props;
    return <Tooltip title={tooltip}>
        <Input value={value} onChange={onChange} />
    </Tooltip>
}

export default function EditModal(props) {
    const { record, node } = props;
    const [form] = Form.useForm();
    const [loading, setLoading] = useState();

    const { message } = App.useApp();

    useEffect(() => {
        if (record) {
            form.setFieldsValue(record);
        }

    }, [form, record])

    return <Modal confirmLoading={loading} width={1000} onOk={e => {
        form.validateFields().then(values => {
            const data = {
                ...record,
                ...values,
            }
            if (record) {
                setLoading(true);
                XztjApi.update(data).then(resp => {
                    props.onOk?.()
                }).finally(e => {
                    setLoading(false);
                })
            } else {
                setLoading(true);
                XztjApi.add(data).then(resp => {
                    props.onOk?.()
                }).finally(e => {
                    setLoading(false);
                })
            }
        }).catch(err => {
            message.error(`${err?.errorFields?.[0]?.errors?.[0] || '验证失败'}`)
        })
    }} bodyStyle={{
        height: 600,
        padding: 24,
        overflow: 'auto'
    }} title={`${record ? "编辑" : "新增"}`} open={props.open} onCancel={e => props.onCancel?.()}>
        <Form form={form} labelAlign='right' labelCol={{ span: 6 }} labelWrap={{ span: 18 }}>

            <Row gutter={24}>
                {getFields(['地址_县', '地址_乡', '农作物总播种面积', '小麦播种面积', '玉米播种面积', '水稻播种面积', '行政区域面积', '特色农作物种类',
                    '高粱播种面积', '大豆播种面积', '马铃薯播种面积', '甘薯播种面积', '花生播种面积',
                    '油菜籽播种面积', '棉花播种面积', '甘蔗播种面积', '甜菜播种面积', '烟叶播种面积', '烤烟播种面积', '蔬菜播种面积',
                    '茶叶播种面积', '药材播种面积', '青稞播种面积', '西瓜播种面积', '柑桔播种面积', '苹果播种面积', '梨播种面积', '葡萄播种面积',
                    '甜瓜播种面积', '香蕉播种面积', '红枣播种面积', '草莓播种面积', '柿子播种面积', '菠萝播种面积'
                ]).map(item => {
                    let node = <InputNumber style={{ width: '100%' }} />
                    switch (item.type) {
                        case 'input':
                            node = <Input />;
                            break;
                        default:
                    }
                    return <Col span={12}>
                        <Form.Item label={item.title} name={item.dataIndex}>
                            {node}
                        </Form.Item>
                    </Col>
                })}

            </Row>
        </Form>
    </Modal>;
}